package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.ElementConfig;
import com.sghd.fight.module.game.config.ElementPk;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.room.Player;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-11-28 11:21
 * @desc 灼烧
 **/
@Component
public class BurnEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> level2Burn = MapUtils.getMap(ctx, String.valueOf(level));

        int val = MapUtils.getIntValue(level2Burn, Effect.TARGET);
        val = (int) (val * getBet(owner, target));
        if (owner.getId() == target.getId()) {
            //对自己造成伤害
            val = MapUtils.getIntValue(level2Burn, Effect.OWNER);
            //对自己减伤害
            val = deHarm(target, config, room, val);
        //敌方
        }else{
            val = fixHp(owner, target, val, room, config);
        }
        context.addValue(target, UnitValue.HP, -val);
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -val));
    }

    private int deHarm(Unit target, SkillConfig config, IRoom room, int val) {
        //减伤
        if (target instanceof Player && config.getType() != null) {
            Map<ChessType, Integer> attr2Level = ((Player) target).getElement2Level();
            int level = attr2Level.get(config.getType());
            for (; level > 0; level--) {
                ElementPk pk = ElementPk.valueOf(config.getType(), level);
                ElementConfig attrConfig = room.getConfigService().getAttr(pk);
                if (logger.isDebugEnabled()) {
                    logger.debug("玩家[{}]五行额外减少伤害：{}", target.getId(), attrConfig.getAttr(room.getType()).getDeHarm());
                }
                val -= attrConfig.getAttr(room.getType()).getDeHarm();
            }
        }
        return val < 0 ? 0 : val;
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_BURN;
    }
}
